'''
Created on Oct 11, 2012

@author: daeheehan

Modified on Oct 31, 2012
@author: gyucheol
'''

from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
from pprint import pprint
import sys
from sets import Set

# Function : deleting items in list
# @param seq The list which has duplicated items
# @return The list which has unique  items
def removeDuplicate(seq):
  seen = set()
  seen_add = seen.add
  # adds all elements it doesn't know yet to seen and all other to seen_twice
  seen_twice = set( x for x in seq if x in seen or seen_add(x) )
  # turn the set into a list (as requested)
  return list( seen_twice )

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = 'skswhwo'
GITHUB_USER_ID_4 = 'luispigu'

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = "wasd1234"

if __name__ == '__main__':
    pass

#gh = login(GITHUB_USER_ID, password=GITHUB_USER_PW)
#print gh.ratelimit_remaining, 'request remaining'

# jQuery_mobile changer commenter information file
GRAPH_FILE = 'jquery_mobile_testset_changer_commenter_time.txt'

# jQuery_mobile changer commenter information file
GRAPH_PLAIN_FILE = 'jquery_mobile_plain_graph.txt'

# Training-Set Boundary
TRAINING_ISSUE = '3361'

list_merged_record = []

# Source-Destination(String, Set) Dictionary
source_destination = {}

try:
    # Symfony project pull requester list from files
    # Id, Open or Close, Merged or Not, Created at, Time Taken to Completion, Addition/Deletion/Changed_lines, Pull Requester, Committer(Merged by), Commenters
    f_pullrequester = open(GRAPH_FILE, 'r')
    
    for line in f_pullrequester:
        print line
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            if(tuple_list[0].strip() == 'Id'):
                continue
            
            if(tuple_list[0].strip() == TRAINING_ISSUE):
                break

            isMerged = tuple_list[2].strip()
            if( isMerged == 'False' ):
                print isMerged
#                continue
            else:
                list_merged_record.append(line.rstrip())
#                print line
#        break
    
    f_pullrequester.close()

#    print 'Only Merged Event'
#    f_followers = open(GRAPH_MERGED_PR_FILE, 'w')
#    for tuple in list_merged_record:
#        sys.stdout.write(str(tuple) + '\n')
#        f_followers.write(str(tuple) + '\n')
#
#    f_followers.close()
    print len(list_merged_record)
    
    print 'File Output finished'
    # Id, Open/Close, Merged or Not, Created at, Time Taken to Completion, Addition/Deletion/Changed_lines, Pull Requester, Committer(Merged by), Commenters
    for tuple in list_merged_record:
        print tuple

        if not tuple.strip():
            continue
        # We got something, save it
        else:
            tuple_tokens = tuple.split(',')
#            print type(tuple_tokens)
#            print len(tuple_tokens)

            if(tuple_tokens[0].strip() == 'Id'):
                continue
            
            source = tuple_tokens[6].strip()
            destinations = tuple_tokens[7:]
            print source, destinations
            
            commenters = Set()
            for des in destinations:
                if(des.strip() == source or des.strip() == 'None'):
                    continue
                else:
                    commenters.add(des.strip())

            # Exist key
            if source in source_destination:
                print ('Exist key')
                previous_commenters = source_destination[source]
                source_destination[source] = previous_commenters + list(commenters)
            else:
                print ('Not Exist key')
                source_destination[source] = list(commenters)
            
#            print commenters
                        
#        break

    print 'Printing dictionary: Source-Destinations'
    f_graph = open(GRAPH_PLAIN_FILE, 'w')
    for key, value in sorted(source_destination.items()):
#        print(key, value)
#        print type(key), type(value)
        f_graph.write(str(key) + ', '+ ', '.join(value) + '\n')
    
    f_graph.close()

except Exception as e: 
    print 'The final Exception', e.args[0]

#print gh.ratelimit_remaining, 'request remaining'

print
print 'Finally the end'